package studychinese.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.jaeger.library.StatusBarUtil;

import studychinese.fragment.ChineseCharacterFragment;
import studychinese.fragment.CorrectPronunciationFragment;
import studychinese.fragment.MyFragment;
import studychinese.fragment.StudyFragment;
import studychinese.utils.AppUtils;

public class HomeActivity extends BaseAcitivity implements View.OnClickListener  {

    // 初始化顶部栏显示
    private ImageView titleLeftImv;
    private TextView titleTv;
    // 定义4个Fragment对象
    private StudyFragment fg1;
    private CorrectPronunciationFragment fg2;
    private ChineseCharacterFragment fg3;
    private MyFragment fg4;
    // 帧布局对象，用来存放Fragment对象
    private FrameLayout frameLayout;
    // 定义每个选项中的相关控件
    private RelativeLayout firstLayout;
    private RelativeLayout secondLayout;
    private RelativeLayout thirdLayout;
    private RelativeLayout fourthLayout;
    private ImageView firstImage;
    private ImageView secondImage;
    private ImageView thirdImage;
    private ImageView fourthImage;
    private LinearLayout ll_back;
    // 定义FragmentManager对象管理器
    private FragmentManager fragmentManager;


    @Override
    protected int setContainer() {
        return R.layout.activity_main1;
    }



    @Override
    public void innitView() {

        fragmentManager = getSupportFragmentManager();
        initView(); // 初始化界面控件
        setChioceItem(0); // 初始化页面加载时显示第一个选项卡
    }
    /**
     * 监听Back键按下事件,方法1:
     * 注意:
     * super.onBackPressed()会自动调用finish()方法,关闭
     * 当前Activity.
     * 若要屏蔽Back键盘,注释该行代码即可
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        AppUtils.exitApp(this);
    }
    /**
     * 初始化页面
     */
    private void initView() {

// 初始化底部导航栏的控件
        firstImage = (ImageView) findViewById(R.id.first_image);
        secondImage = (ImageView) findViewById(R.id.second_image);
        thirdImage = (ImageView) findViewById(R.id.third_image);
        fourthImage = (ImageView) findViewById(R.id.fourth_image);

        firstLayout = (RelativeLayout) findViewById(R.id.first_layout);
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        secondLayout = (RelativeLayout) findViewById(R.id.second_layout);
        thirdLayout = (RelativeLayout) findViewById(R.id.third_layout);
        fourthLayout = (RelativeLayout) findViewById(R.id.fourth_layout);
        firstLayout.setOnClickListener(HomeActivity.this);
        secondLayout.setOnClickListener(HomeActivity.this);
        thirdLayout.setOnClickListener(HomeActivity.this);
        fourthLayout.setOnClickListener(HomeActivity.this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.first_layout:
                setChioceItem(0);
                break;
            case R.id.second_layout:
                setChioceItem(1);
                break;
            case R.id.third_layout:
                setChioceItem(2);
                break;
            case R.id.fourth_layout:
                setChioceItem(3);
                break;
            default:
                break;
        }
    }
    /**
     * 设置点击选项卡的事件处理
     *
     * @param index 选项卡的标号：0, 1, 2, 3
     */
    private void setChioceItem(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        clearChioce(); // 清空, 重置选项, 隐藏所有Fragment
        hideFragments(fragmentTransaction);
        switch (index) {
            case 0:
 firstImage.setImageResource(R.mipmap.bot_student_click);


// 如果fg1为空，则创建一个并添加到界面上
                if (fg1 == null) {
                    fg1 = new StudyFragment();
                    fragmentTransaction.add(R.id.content, fg1);
                } else {
// 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(fg1);
                }
                break;
            case 1:
 secondImage.setImageResource(R.mipmap.bot_sound_click);


                if (fg2 == null) {
                    fg2 = new CorrectPronunciationFragment();
                    fragmentTransaction.add(R.id.content, fg2);
                } else {
                    fragmentTransaction.show(fg2);
                }
                break;
            case 2:
 thirdImage.setImageResource(R.mipmap.bot_chinese_click);


                if (fg3 == null) {
                    fg3 = new ChineseCharacterFragment();
                    fragmentTransaction.add(R.id.content, fg3);
                } else {
                    fragmentTransaction.show(fg3);
                }
                break;
            case 3:
 fourthImage.setImageResource(R.mipmap.bot_my_click);


                if (fg4 == null) {
                    fg4 = new MyFragment();
                    fragmentTransaction.add(R.id.content, fg4);
                } else {
                    fragmentTransaction.show(fg4);
                }
                break;
        }
        fragmentTransaction.commit(); // 提交
    }
    /**
     * 当选中其中一个选项卡时，其他选项卡重置为默认
     */
    private void clearChioce() {
        firstImage.setImageResource(R.mipmap.bot_student_unclick);
        secondImage.setImageResource(R.mipmap.bot_sound_unclick);
        thirdImage.setImageResource(R.mipmap.bot_chinese);
        fourthImage.setImageResource(R.mipmap.bot_my_unclick);
    }
    /**
     * 隐藏Fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (fg1 != null) {
            fragmentTransaction.hide(fg1);
        }
        if (fg2 != null) {
            fragmentTransaction.hide(fg2);
        }
        if (fg3 != null) {
            fragmentTransaction.hide(fg3);
        }
        if (fg4 != null) {
            fragmentTransaction.hide(fg4);
        }
    }
    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0,null);
    }
}
